home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevx.c < prev    next >
C/C++ Source or Header  |  1993-05-28  |  32KB  |  1,003 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevx.c */
  20. /* X Windows driver for Ghostscript library */
  21. /* The X include files include <sys/types.h>, which, on some machines */
  22. /* at least, define uint, ushort, and ulong, which std.h also defines. */
  23. /* std.h has taken care of this. */
  24. #include "gx.h"            /* for gx_bitmap; includes std.h */
  25. #include "memory_.h"
  26. #include "x_.h"
  27. #include "gserrors.h"
  28. #include "gsprops.h"
  29. #include "gsutil.h"        /* for props_extract */
  30. #include "gxdevice.h"
  31. #include "gdevx.h"
  32.  
  33. /* Define the maximum size of the temporary pixmap for copy_mono */
  34. /* that we are willing to leave lying around in the server */
  35. /* between uses.  (Assume 32-bit ints here!) */
  36. private int max_temp_pixmap = 20000;
  37.  
  38. /* Forward references */
  39. private int set_tile(P2(gx_device *, const gx_bitmap *));
  40. private void free_cp(P1(gx_device *));
  41. /* Screen updating machinery */
  42. #define update_init(dev)\
  43.   ((gx_device_X *)(dev))->up_area = 0,\
  44.   ((gx_device_X *)(dev))->up_count = 0
  45. #define update_flush(dev)\
  46.   if ( ((gx_device_X *)(dev))->up_area != 0 ) update_do_flush(dev)
  47. private void update_do_flush(P1(gx_device *));
  48. private void x_send_event(P2(gx_device *, Atom));
  49.  
  50. /* Procedures */
  51.  
  52. extern int gdev_x_open(P1(gx_device_X *));
  53. private dev_proc_open_device(x_open);
  54. private dev_proc_get_initial_matrix(x_get_initial_matrix);
  55. private dev_proc_sync_output(x_sync);
  56. private dev_proc_output_page(x_output_page);
  57. private dev_proc_close_device(x_close);
  58. private dev_proc_map_rgb_color(x_map_rgb_color);
  59. private dev_proc_map_color_rgb(x_map_color_rgb);
  60. private dev_proc_fill_rectangle(x_fill_rectangle);
  61. private dev_proc_tile_rectangle(x_tile_rectangle);
  62. private dev_proc_copy_mono(x_copy_mono);
  63. private dev_proc_copy_color(x_copy_color);
  64. private dev_proc_draw_line(x_draw_line);
  65. private dev_proc_put_props(x_put_props);
  66. dev_proc_get_xfont_procs(x_get_xfont_procs);
  67.  
  68. /* The device descriptor */
  69. private gx_device_procs x_procs = {
  70.     x_open,
  71.     x_get_initial_matrix,
  72.     x_sync,
  73.     x_output_page,
  74.     x_close,
  75.     x_map_rgb_color,
  76.     x_map_color_rgb,
  77.     x_fill_rectangle,
  78.     x_tile_rectangle,
  79.     x_copy_mono,
  80.     x_copy_color,
  81.     x_draw_line,
  82.     gx_default_get_bits,
  83.     gx_default_get_props,
  84.     x_put_props,
  85.     NULL,
  86.     x_get_xfont_procs
  87. };
  88.  
  89. /* The instance is public. */
  90. gx_device_X gs_x11_device = {
  91.     sizeof(gx_device_X),
  92.     &x_procs,
  93.     "x11",
  94.     (int)(8.5*FAKE_RES),
  95.     (int)(11*FAKE_RES),    /* x and y extent (nominal) */
  96.     FAKE_RES, FAKE_RES,    /* x and y density (nominal) */
  97.     no_margins,
  98.     dci_black_and_white,
  99.     0,            /* connection not initialized */
  100.     { /* image */
  101.       0, 0,            /* width, height */
  102.       0, XYBitmap, NULL,    /* xoffset, format, data */
  103.       LSBFirst, 8,        /* byte-order, bitmap-unit */
  104.       MSBFirst, 8, 1,    /* bitmap-bit-order, bitmap-pad, depth */
  105.       0, 1,            /* bytes_per_line, bits_per_pixel */
  106.       0, 0, 0,        /* red_mask, green_mask, blue_mask */
  107.       NULL,            /* *obdata */
  108.        { NULL,            /* *(*create_image)() */
  109.          NULL,            /* (*destroy_image)() */
  110.          NULL,            /* (*get_pixel)() */
  111.          NULL,            /* (*put_pixel)() */
  112.          NULL,            /* *(*sub_image)() */
  113.          NULL            /* (*add_pixel)() */
  114.        },
  115.     },
  116.     NULL, NULL,        /* dpy, scr */
  117.                 /* (connection not initialized) */
  118.     NULL,            /* vinfo */
  119.     (Colormap)None,        /* cmap */
  120.     (Window)None,        /* win */
  121.     NULL,            /* gc */
  122.     (Pixmap)0,        /* bpixmap */
  123.     0,            /* ghostview */
  124.     (Window)None,        /* mwin */
  125. #if HaveStdCMap
  126.     NULL,            /* std_cmap */
  127. #endif
  128.     identity_matrix_body,    /* initial matrix (filled in) */
  129.     (Atom)0, (Atom)0, (Atom)0,    /* Atoms: NEXT, PAGE, DONE */
  130.      { 0, 0, 0, 0 }, 0, 0,    /* update, up_area, up_count */
  131.     (Pixmap)0,        /* dest */
  132.     0L, ~0L,        /* colors_or, colors_and */
  133.      { /* cp */
  134.        (Pixmap)0,        /* pixmap */
  135.        NULL,        /* gc */
  136.        -1, -1        /* raster, height */
  137.      },
  138.      { /* ht */
  139.        (Pixmap)None,        /* pixmap */
  140.        (Pixmap)None,        /* no_pixmap */
  141.        gx_no_bitmap_id,        /* id */
  142.        0, 0, 0,            /* width, height, raster */
  143.        0, 0                /* fore_c, back_c */
  144.      },
  145.     GXcopy,            /* function */
  146.     FillSolid,        /* fill_style */
  147.     0,            /* font */
  148.     0, 0,            /* back_color, fore_color */
  149.     0, 0,            /* background, foreground */
  150.     NULL,            /* dither_colors */
  151.     0, 0,            /* color_mask, half_dev_color */
  152.     NULL,            /* dynamic_colors */
  153.     0, 0,            /* dynamic_size, dynamic_number */
  154.     0, 0,            /* borderColor, borderWidth */
  155.     NULL,            /* geometry */
  156.     128, 5,            /* maxGrayRamp, maxRGBRamp */
  157.     NULL,            /* palette */
  158.     NULL, NULL, NULL,    /* regularFonts, symbolFonts, dingbatFonts */
  159.     NULL, NULL, NULL,    /* regular_fonts, symbol_fonts, dingbat_fonts */
  160.     1, 1, 0,        /* useXFonts, useScalableFonts, logXFonts */
  161.     10.0,            /* xFontTolerance */
  162.     0.0, 0.0,        /* xResolution, yResolution */
  163.     1,            /* useBackingPixmap */
  164.     1, 1,            /* useXPutImage, useXSetTile */
  165. };
  166.  
  167. /* If XPutImage doesn't work, do it ourselves. */
  168. private void alt_put_image();
  169. #define put_image(dpy,win,gc,im,sx,sy,x,y,w,h)\
  170.   if ( xdev->useXPutImage) XPutImage(dpy,win,gc,im,sx,sy,x,y,w,h);\
  171.   else alt_put_image(dev,dpy,win,gc,im,sx,sy,x,y,w,h)
  172.  
  173.  
  174. /* Open the device.  Most of the code is in gdevxini.c. */
  175. private int
  176. x_open(gx_device *dev)
  177. {
  178.     gx_device_X *xdev = (gx_device_X *)dev;
  179.     int code = gdev_x_open(xdev);
  180.  
  181.     if (code < 0) return code;
  182.     update_init(dev);
  183.     return 0;
  184. }
  185.  
  186. /* Close the device. */
  187. private int
  188. x_close(gx_device *dev)
  189. {
  190.     gx_device_X *xdev = (gx_device_X *)dev;
  191.  
  192.     if (xdev->ghostview) x_send_event(dev, xdev->done);
  193.     if (xdev->vinfo) {
  194.     XFree(xdev->vinfo);
  195.     xdev->vinfo = NULL;
  196.     }
  197.     if (xdev->dither_colors) {
  198.     if (gx_device_has_color(xdev))
  199.         gs_free((char *)xdev->dither_colors, sizeof(x_pixel),
  200.             xdev->color_info.dither_gray, "gdev_x_gray_ramp");
  201.     else
  202.         gs_free((char *)xdev->dither_colors, sizeof(x_pixel),
  203.             xdev->color_info.dither_rgb, "gdev_x_rgb_cube");
  204.     xdev->dither_colors = NULL;
  205.     }
  206.     if (xdev->dynamic_colors) {
  207.     gs_free((char *)xdev->dynamic_colors, sizeof(XColor),
  208.         xdev->dynamic_size, "gdev_x_dynamic_colors");
  209.     xdev->dynamic_colors = NULL;
  210.     }
  211.     while (xdev->regular_fonts) {
  212.     x11fontmap *font = xdev->regular_fonts;
  213.     xdev->regular_fonts = font->next;
  214.     if (font->std_names) XFreeFontNames(font->std_names);
  215.     if (font->iso_names) XFreeFontNames(font->iso_names);
  216.     gs_free(font->x11_name, sizeof(char), strlen(font->x11_name)+1,
  217.         "gdev_x_font_x11name");
  218.     gs_free(font->ps_name, sizeof(char), strlen(font->ps_name)+1,
  219.         "gdev_x_font_psname");
  220.     gs_free((char *)font, sizeof(x11fontmap), 1, "gdev_x_fontmap");
  221.     }
  222.     while (xdev->symbol_fonts) {
  223.     x11fontmap *font = xdev->symbol_fonts;
  224.     xdev->symbol_fonts = font->next;
  225.     if (font->std_names) XFreeFontNames(font->std_names);
  226.     if (font->iso_names) XFreeFontNames(font->iso_names);
  227.     gs_free(font->x11_name, sizeof(char), strlen(font->x11_name)+1,
  228.         "gdev_x_font_x11name");
  229.     gs_free(font->ps_name, sizeof(char), strlen(font->ps_name)+1,
  230.         "gdev_x_font_psname");
  231.     gs_free((char *)font, sizeof(x11fontmap), 1, "gdev_x_fontmap");
  232.     }
  233.     while (xdev->dingbat_fonts) {
  234.     x11fontmap *font = xdev->dingbat_fonts;
  235.     xdev->dingbat_fonts = font->next;
  236.     if (font->std_names) XFreeFontNames(font->std_names);
  237.     if (font->iso_names) XFreeFontNames(font->iso_names);
  238.     gs_free(font->x11_name, sizeof(char), strlen(font->x11_name)+1,
  239.         "gdev_x_font_x11name");
  240.     gs_free(font->ps_name, sizeof(char), strlen(font->ps_name)+1,
  241.         "gdev_x_font_psname");
  242.     gs_free((char *)font, sizeof(x11fontmap), 1, "gdev_x_fontmap");
  243.     }
  244.     XCloseDisplay(xdev->dpy);
  245.     return 0;
  246. }
  247.  
  248. /* Map a color.  The "device colors" are just r,g,b packed together. */
  249. private gx_color_index
  250. x_map_rgb_color(register gx_device *dev,
  251.         gx_color_value r, gx_color_value g, gx_color_value b)
  252. {
  253.     gx_device_X *xdev = (gx_device_X *)dev;
  254.     unsigned short dr = r & xdev->color_mask;    /* Nearest color that */
  255.     unsigned short dg = g & xdev->color_mask;    /* the X device can   */
  256.     unsigned short db = b & xdev->color_mask;    /* represent          */
  257.  
  258.     /* foreground and background get special treatment */
  259.     /* They maybe mapped to other colors. */
  260. #define cv_max (gx_max_color_value & xdev->color_mask)
  261.     if (dr == 0 && dg == 0 && db == 0) {
  262.     return xdev->foreground;
  263.     }
  264.     if (dr == cv_max && dg == cv_max && db == cv_max) {
  265.     return xdev->background;
  266.     }
  267. #define cv_denom (gx_max_color_value + 1)
  268. #if HaveStdCMap
  269.     /* check the standard colormap first */
  270.     if (xdev->std_cmap) {
  271.     XStandardColormap *cmap = xdev->std_cmap;
  272.  
  273.     if (gx_device_has_color(xdev)) {
  274.         unsigned short cr, cg, cb;        /* rgb cube indices */
  275.         unsigned short cvr, cvg, cvb;    /* color value on cube */
  276.  
  277.         cr = r * (cmap->red_max + 1) / cv_denom;
  278.         cg = g * (cmap->green_max + 1) / cv_denom;
  279.         cb = b * (cmap->blue_max + 1) / cv_denom;
  280.         cvr = (0xffff * cr / cmap->red_max);
  281.         cvg = (0xffff * cg / cmap->green_max);
  282.         cvb = (0xffff * cb / cmap->blue_max);
  283.         if (abs((int)r - (int)cvr) < xdev->half_dev_color &&
  284.         abs((int)g - (int)cvg) < xdev->half_dev_color &&
  285.         abs((int)b - (int)cvb) < xdev->half_dev_color)
  286.         return cr * cmap->red_mult + cg * cmap->green_mult +
  287.                cb * cmap->blue_mult + cmap->base_pixel;
  288.     } else {
  289.         unsigned short cr;
  290.         unsigned short cvr;
  291.  
  292.         cr = r * (xdev->color_info.max_gray + 1) / cv_denom;
  293.         cvr = (0xffff * cr / cmap->red_max);
  294.         if (abs((int)r - (int)cvr) < xdev->half_dev_color)
  295.         return cr * cmap->red_mult + cmap->base_pixel;
  296.     }
  297.     } else
  298. #endif
  299.     /* If there is no standard colormap, check the dither cube/ramp */
  300.     if (xdev->dither_colors) {
  301.     if (gx_device_has_color(xdev)) {
  302.         unsigned short cr, cg, cb;        /* rgb cube indices */
  303.         unsigned short cvr, cvg, cvb;    /* color value on cube */
  304.  
  305. #define max_rgb (xdev->color_info.dither_rgb - 1)
  306.         cr = r * (xdev->color_info.dither_rgb) / cv_denom;
  307.         cg = g * (xdev->color_info.dither_rgb) / cv_denom;
  308.         cb = b * (xdev->color_info.dither_rgb) / cv_denom;
  309.         cvr = (0xffff * cr / max_rgb);
  310.         cvg = (0xffff * cg / max_rgb);
  311.         cvb = (0xffff * cb / max_rgb);
  312.         if (abs((int)r - (int)cvr) < xdev->half_dev_color &&
  313.         abs((int)g - (int)cvg) < xdev->half_dev_color &&
  314.         abs((int)b - (int)cvb) < xdev->half_dev_color) {
  315.         return xdev->dither_colors[cube_index(cr, cg, cb)];
  316.         }
  317. #undef max_rgb
  318.     } else {
  319.         unsigned short cr;
  320.         unsigned short cvr;
  321.  
  322. #define max_gray (xdev->color_info.dither_gray - 1)
  323.         cr = r * (xdev->color_info.dither_gray) / cv_denom;
  324.         cvr = (0xffff * cr / max_gray) & xdev->color_mask;
  325.         if (abs((int)r - (int)cvr) < xdev->half_dev_color)
  326.         return xdev->dither_colors[cr];
  327. #undef max_gray
  328.     }
  329.     }
  330. #undef cv_denom
  331.     /* Finally look through the list of dynamic colors */
  332.     if (xdev->dynamic_colors) {
  333.     int i;
  334.     XColor *xcp = xdev->dynamic_colors;
  335.     XColor xc;
  336.  
  337.     for (i = 0; i < xdev->dynamic_number; xcp++, i++) {
  338.         if (xcp->red == dr && xcp->green == dg && xcp->blue == db) {
  339.         return xcp->pixel;
  340.         }
  341.     }
  342.  
  343.     /* If not in our list of dynamic colors, ask the X server and */
  344.     /* add an entry if there is room. */
  345.     xc.red = dr;
  346.     xc.green = dg;
  347.     xc.blue = db;
  348.     if (XAllocColor(xdev->dpy, xdev->cmap, &xc)) {
  349.         if (xdev->dynamic_number < xdev->dynamic_size) {
  350.         xcp->red = dr;
  351.         xcp->green = dg;
  352.         xcp->blue = db;
  353.         xcp->pixel = xc.pixel;
  354.         xdev->dynamic_number++;
  355.         }
  356.         return xc.pixel;
  357.     } else {
  358.         return gx_no_color_index;
  359.     }
  360.     }
  361.     return gx_no_color_index;
  362. }
  363.  
  364.  
  365. /* Map a "device color" back to r-g-b. */
  366. /* This doesn't happed often, so we just ask the display */
  367. /* Foreground and background be mapped to other colors, so */
  368. /* they are handled specially. */
  369. private int
  370. x_map_color_rgb(register gx_device *dev, gx_color_index color,
  371.         gx_color_value prgb[3])
  372. {
  373.     gx_device_X *xdev = (gx_device_X *)dev;
  374.  
  375.     if (color == xdev->foreground)
  376.     prgb[0] = prgb[1] = prgb[2] = 0;
  377.     else if (color == xdev->background)
  378.     prgb[0] = prgb[1] = prgb[2] = gx_max_color_value;
  379.     else {
  380.     XColor xc;
  381.  
  382.     xc.pixel = color;
  383.     XQueryColor(xdev->dpy, xdev->cmap, &xc);
  384.     prgb[0] = xc.red;
  385.     prgb[1] = xc.green;
  386.     prgb[2] = xc.blue;
  387.     }
  388.     return 0;
  389. }
  390.  
  391. /* Get initial matrix for X device */
  392. private void
  393. x_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  394. {
  395.     gx_device_X *xdev = (gx_device_X *)dev;
  396.  
  397.     pmat->xx = xdev->initial_matrix.xx;
  398.     pmat->xy = xdev->initial_matrix.xy;
  399.     pmat->yx = xdev->initial_matrix.yx;
  400.     pmat->yy = xdev->initial_matrix.yy;
  401.     pmat->tx = xdev->initial_matrix.tx;
  402.     pmat->ty = xdev->initial_matrix.ty;
  403. }
  404.  
  405. /* Synchronize the display with the commands already given */
  406. private int
  407. x_sync(register gx_device *dev)
  408. {
  409.     gx_device_X *xdev = (gx_device_X *)dev;
  410.  
  411.     update_flush(dev);
  412.     XSync(xdev->dpy, 0);
  413.     return 0;
  414. }
  415.  
  416. /* Send event to ghostview process */
  417. private void
  418. x_send_event(gx_device *dev, Atom msg)
  419. {
  420.     gx_device_X *xdev = (gx_device_X *)dev;
  421.     XEvent event;
  422.  
  423.     event.xclient.type = ClientMessage;
  424.     event.xclient.display = xdev->dpy;
  425.     event.xclient.window = xdev->win;
  426.     event.xclient.message_type = msg;
  427.     event.xclient.format = 32;
  428.     event.xclient.data.l[0] = xdev->mwin;
  429.     event.xclient.data.l[1] = xdev->dest;
  430.     XSendEvent(xdev->dpy, xdev->win, False, 0, &event);
  431. }
  432.  
  433. /* Output "page" */
  434. private int
  435. x_output_page(gx_device *dev, int num_copies, int flush)
  436. {
  437.     gx_device_X *xdev = (gx_device_X *)dev;
  438.  
  439.     x_sync(dev);
  440.  
  441.     /* Send ghostview a "page" client event */
  442.     /* Wait for a "next" client event */
  443.     if (xdev->ghostview) {
  444.     XEvent event;
  445.  
  446.     x_send_event(dev, xdev->page);
  447.     XNextEvent(xdev->dpy, &event);
  448.     while (event.type != ClientMessage ||
  449.            event.xclient.message_type != xdev->next) {
  450.         XNextEvent(xdev->dpy, &event);
  451.     }
  452.     }
  453.     return 0;
  454. }
  455.  
  456. /* Fill a rectangle with a color. */
  457. private int
  458. x_fill_rectangle(register gx_device *dev,
  459.          int x, int y, int w, int h, gx_color_index color)
  460. {
  461.     gx_device_X *xdev = (gx_device_X *)dev;
  462.  
  463.     fit_fill(dev, x, y, w, h);
  464.     set_fill_style(FillSolid);
  465.     set_fore_color(color);
  466.     set_function(GXcopy);
  467.     XFillRectangle(xdev->dpy, xdev->dest, xdev->gc, x, y, w, h);
  468.     /* If we are filling the entire screen, reset */
  469.     /* colors_or and colors_and.  It's wasteful to do this */
  470.     /* on every operation, but there's no separate driver routine */
  471.     /* for erasepage (yet). */
  472.     if (x == 0 && y == 0 && w == xdev->width && h == xdev->height) {
  473.     xdev->colors_or = xdev->colors_and = color;
  474.     }
  475.     if (xdev->bpixmap != (Pixmap) 0) {
  476.     x_update_add(dev, x, y, w, h);
  477.     }
  478. #ifdef DEBUG
  479.     if (gs_debug['F'])
  480.     dprintf5("[F] fill (%d,%d):(%d,%d) %ld\n",
  481.          x, y, w, h, (long)color);
  482. #endif
  483.     return 0;
  484. }
  485.  
  486. /* Tile a rectangle. */
  487. private int
  488. x_tile_rectangle(register gx_device *dev, const gx_bitmap *tile,
  489.            int x, int y, int w, int h,
  490.          gx_color_index zero, gx_color_index one,
  491.          int px, int py)
  492. {
  493.     gx_device_X *xdev = (gx_device_X *)dev;
  494.  
  495.     fit_fill(dev, x, y, w, h);
  496.  
  497.     /* Check for a colored tile.  We should implement this */
  498.     /* properly someday, since X can handle it. */
  499.  
  500.     if (one == gx_no_color_index && zero == gx_no_color_index)
  501.     return -1;
  502.  
  503.     /* For the moment, give up if the phase is non-zero. */
  504.     if (px | py)
  505.     return -1;
  506.  
  507.     /*
  508.      * Remember, an X tile is already filled with particular
  509.      * pixel values (i.e., colors).  Therefore if we are changing
  510.      * fore/background color, we must invalidate the tile (using
  511.      * the same technique as in set_tile).  This problem only
  512.      * bites when using grayscale -- you may want to change
  513.      * fg/bg but use the same halftone screen.
  514.      */
  515.     if ((zero != xdev->ht.back_c) || (one != xdev->ht.fore_c))
  516.     xdev->ht.id = ~tile->id;    /* force reload */
  517.  
  518.     set_back_color(zero);
  519.     set_fore_color(one);
  520.     if (!set_tile(dev, tile)) {
  521.     /* Bad news.  Fall back to the default algorithm. */
  522.     return gx_default_tile_rectangle(dev, tile, x, y, w, h,
  523.                      zero, one, px, py);
  524.     } else {    /* Use the tile to fill the rectangle */
  525.     set_fill_style(FillTiled);
  526.     set_function(GXcopy);
  527.     XFillRectangle(xdev->dpy, xdev->dest, xdev->gc, x, y, w, h);
  528.     if (xdev->bpixmap != (Pixmap) 0) {
  529.         x_update_add(dev, x, y, w, h);
  530.     }
  531.     }
  532. #ifdef DEBUG
  533.     if (gs_debug['F'])
  534.     dprintf6("[F] tile (%d,%d):(%d,%d) %ld,%ld\n",
  535.          x, y, w, h, (long)zero, (long)one);
  536. #endif
  537.     return 0;
  538. }
  539.  
  540. /* Set up with a specified tile. */
  541. /* Return false if we can't do it for some reason. */
  542. private int
  543. set_tile(register gx_device *dev, register const gx_bitmap *tile)
  544. {
  545.     gx_device_X *xdev = (gx_device_X *)dev;
  546.  
  547. #ifdef DEBUG
  548.     if (gs_debug['T'])
  549.     return 0;
  550. #endif
  551.     if (tile->id == xdev->ht.id && tile->id != gx_no_bitmap_id)
  552.     return xdev->useXSetTile;
  553.     /* Set up the tile Pixmap */
  554.     if (tile->size.x != xdev->ht.width ||
  555.     tile->size.y != xdev->ht.height ||
  556.     xdev->ht.pixmap == (Pixmap) 0) {
  557.     if (xdev->ht.pixmap != (Pixmap) 0)
  558.         XFreePixmap(xdev->dpy, xdev->ht.pixmap);
  559.     xdev->ht.pixmap = XCreatePixmap(xdev->dpy, xdev->win,
  560.                     tile->size.x, tile->size.y,
  561.                     xdev->vinfo->depth);
  562.     if (xdev->ht.pixmap == (Pixmap) 0)
  563.         return 0;
  564.     xdev->ht.width = tile->size.x, xdev->ht.height = tile->size.y;
  565.     xdev->ht.raster = tile->raster;
  566.     }
  567.     xdev->ht.fore_c = xdev->fore_color;
  568.     xdev->ht.back_c = xdev->back_color;
  569.     /* Copy the tile into the Pixmap */
  570.     xdev->image.data = (char *)tile->data;
  571.     xdev->image.width = tile->size.x;
  572.     xdev->image.height = tile->size.y;
  573.     xdev->image.bytes_per_line = tile->raster;
  574.     xdev->image.format = XYBitmap;
  575.     set_fill_style(FillSolid);
  576. #ifdef DEBUG
  577.     if (gs_debug['H']) {
  578.     int i;
  579.  
  580.     dprintf4("[H] 0x%x: width=%d height=%d raster=%d\n",
  581.          tile->data, tile->size.x, tile->size.y, tile->raster);
  582.     for (i = 0; i < tile->raster * tile->size.y; i++)
  583.         dprintf1(" %02x", tile->data[i]);
  584.     dputc('\n');
  585.     }
  586. #endif
  587.     XSetTile(xdev->dpy, xdev->gc, xdev->ht.no_pixmap);    /* *** X bug *** */
  588.     set_function(GXcopy);
  589.     put_image(xdev->dpy, xdev->ht.pixmap, xdev->gc, &xdev->image,
  590.           0, 0, 0, 0, tile->size.x, tile->size.y);
  591.     XSetTile(xdev->dpy, xdev->gc, xdev->ht.pixmap);
  592.     xdev->ht.id = tile->id;
  593.     return xdev->useXSetTile;
  594. }
  595.  
  596. /* Copy a monochrome bitmap. */
  597. private int
  598. x_copy_mono(register gx_device *dev,
  599.         const byte *base, int sourcex, int raster, gx_bitmap_id id,
  600.         int x, int y, int w, int h,
  601.         gx_color_index zero, gx_color_index one)
  602. /*
  603.  * X doesn't directly support the simple operation of writing a color
  604.  * through a mask specified by an image.  The plot is the following:
  605.  *  If neither color is gx_no_color_index ("transparent"),
  606.  *    use XPutImage with the "copy" function as usual.
  607.  *  If the color either bitwise-includes or is bitwise-included-in
  608.  *      every color written to date
  609.  *      (a special optimization for writing black/white on color displays),
  610.  *    use XPutImage with an appropriate Boolean function.
  611.  *  Otherwise, do the following complicated stuff:
  612.  *    Create pixmap of depth 1 if necessary.
  613.  *    If foreground color is "transparent" then
  614.  *      invert the raster data.
  615.  *    Use XPutImage to copy the raster image to the newly
  616.  *      created Pixmap.
  617.  *    Install the Pixmap as the clip_mask in the X GC and
  618.  *      tweak the clip origin.
  619.  *    Do an XFillRectangle, fill style=solid, specifying a
  620.  *      rectangle the same size as the original raster data.
  621.  *    De-install the clip_mask.
  622.  */
  623. {
  624.     gx_device_X *xdev = (gx_device_X *)dev;
  625.     int function = GXcopy;
  626.  
  627.     x_pixel
  628.     bc = zero,
  629.     fc = one;
  630.  
  631.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  632.  
  633.     xdev->image.width = raster << 3;
  634.     xdev->image.height = h;
  635.     xdev->image.data = (char *)base;
  636.     xdev->image.bytes_per_line = raster;
  637.     set_fill_style(FillSolid);
  638.  
  639.     /* Check for null, easy 1-color, hard 1-color, and 2-color cases. */
  640.     if (zero != gx_no_color_index) {
  641.     if (one != gx_no_color_index) {
  642.         /* 2-color case. */
  643.         /* Simply replace existing bits with what's in the image. */
  644.     } else if (!(~xdev->colors_and & bc)) {
  645.         function = GXand;
  646.         fc = ~(x_pixel) 0;
  647.     } else if (!(~bc & xdev->colors_or)) {
  648.         function = GXor;
  649.         fc = 0;
  650.     } else {
  651.         goto hard;
  652.     }
  653.     } else {
  654.     if (one == gx_no_color_index) {    /* no-op */
  655.         return 0;
  656.     } else if (!(~xdev->colors_and & fc)) {
  657.         function = GXand;
  658.         bc = ~(x_pixel) 0;
  659.     } else if (!(~fc & xdev->colors_or)) {
  660.         function = GXor;
  661.         bc = 0;
  662.     } else {
  663.         goto hard;
  664.     }
  665.     }
  666.     xdev->image.format = XYBitmap;
  667.     set_function(function);
  668.     if (bc != xdev->back_color) {
  669.     XSetBackground(xdev->dpy, xdev->gc, (xdev->back_color = bc));
  670.     }
  671.     if (fc != xdev->fore_color) {
  672.     XSetForeground(xdev->dpy, xdev->gc, (xdev->fore_color = fc));
  673.     }
  674.     if (zero != gx_no_color_index)
  675.     note_color(zero);
  676.     if (one != gx_no_color_index)
  677.     note_color(one);
  678.     put_image(xdev->dpy, xdev->dest, xdev->gc, &xdev->image,
  679.           sourcex, 0, x, y, w, h);
  680.  
  681.     goto out;
  682.  
  683. hard:    /* Handle the hard 1-color case. */
  684.     if (raster > xdev->cp.raster || h > xdev->cp.height) {
  685.     /* Must allocate a new pixmap and GC. */
  686.     /* Release the old ones first. */
  687.     free_cp(dev);
  688.  
  689.     /* Create the clipping pixmap, depth must be 1. */
  690.     xdev->cp.pixmap =
  691.         XCreatePixmap(xdev->dpy, xdev->win, raster << 3, h, 1);
  692.     if (xdev->cp.pixmap == (Pixmap) 0) {
  693.         lprintf("x_copy_mono: can't allocate pixmap\n");
  694.         exit(1);
  695.     }
  696.     xdev->cp.gc = XCreateGC(xdev->dpy, xdev->cp.pixmap, 0, 0);
  697.     if (xdev->cp.gc == (GC) 0) {
  698.         lprintf("x_copy_mono: can't allocate GC\n");
  699.         exit(1);
  700.     }
  701.     xdev->cp.raster = raster;
  702.     xdev->cp.height = h;
  703.     }
  704.     /* Initialize static mask image params */
  705.     xdev->image.format = ZPixmap;
  706.     set_function(GXcopy);
  707.  
  708.     /* Select polarity based on fg/bg transparency. */
  709.     if (one == gx_no_color_index) {    /* invert */
  710.     XSetBackground(xdev->dpy, xdev->cp.gc, (x_pixel) 1);
  711.     XSetForeground(xdev->dpy, xdev->cp.gc, (x_pixel) 0);
  712.     set_fore_color(zero);
  713.     } else {
  714.     XSetBackground(xdev->dpy, xdev->cp.gc, (x_pixel) 0);
  715.     XSetForeground(xdev->dpy, xdev->cp.gc, (x_pixel) 1);
  716.     set_fore_color(one);
  717.     }
  718.     put_image(xdev->dpy, xdev->cp.pixmap, xdev->cp.gc,
  719.           &xdev->image, sourcex, 0, 0, 0, w, h);
  720.  
  721.     /* Install as clipmask. */
  722.     XSetClipMask(xdev->dpy, xdev->gc, xdev->cp.pixmap);
  723.     XSetClipOrigin(xdev->dpy, xdev->gc, x, y);
  724.  
  725.     /*
  726.      * Draw a solid rectangle through the raster clip mask.
  727.      * Note fill style is guaranteed to be solid from above.
  728.      */
  729.     XFillRectangle(xdev->dpy, xdev->dest, xdev->gc, x, y, w, h);
  730.  
  731.     /* Tidy up.  Free the pixmap if it's big. */
  732.     XSetClipMask(xdev->dpy, xdev->gc, None);
  733.     if (raster * h > max_temp_pixmap)
  734.     free_cp(dev);
  735.  
  736. out:if (xdev->bpixmap != (Pixmap) 0) {
  737.     /* We wrote to the pixmap, so update the display now. */
  738.     x_update_add(dev, x, y, w, h);
  739.     }
  740.     return 0;
  741. }
  742.  
  743. /* Internal routine to free the GC and pixmap used for copying. */
  744. private void
  745. free_cp(register gx_device *dev)
  746. {
  747.     gx_device_X *xdev = (gx_device_X *)dev;
  748.  
  749.     if (xdev->cp.gc != NULL) {
  750.     XFreeGC(xdev->dpy, xdev->cp.gc);
  751.     xdev->cp.gc = NULL;
  752.     }
  753.     if (xdev->cp.pixmap != (Pixmap) 0) {
  754.     XFreePixmap(xdev->dpy, xdev->cp.pixmap);
  755.     xdev->cp.pixmap = (Pixmap) 0;
  756.     }
  757.     xdev->cp.raster = -1;    /* mark as unallocated */
  758. }
  759.  
  760. /* Copy a "color" bitmap.  Since "color" is the same as monochrome, */
  761. /* this just reduces to copying a monochrome bitmap. */
  762. /****** THIS ROUTINE IS COMPLETELY WRONG, SINCE WE DO SUPPORT COLOR. ******/
  763. /* Fortunately, no one uses it at the moment. */
  764. private int
  765. x_copy_color(register gx_device *dev,
  766.          const byte *base, int sourcex, int raster, gx_bitmap_id id,
  767.          int x, int y, int w, int h)
  768. {
  769.     gx_device_X *xdev = (gx_device_X *)dev;
  770.  
  771.     return x_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  772.                xdev->foreground, xdev->background);
  773. }
  774.  
  775. /* Draw a line */
  776. private int
  777. x_draw_line(register gx_device *dev,
  778.         int x0, int y0, int x1, int y1, gx_color_index color)
  779. {
  780.     gx_device_X *xdev = (gx_device_X *)dev;
  781.  
  782.     set_fore_color(color);
  783.     set_fill_style(FillSolid);
  784.     set_function(GXcopy);
  785.     XDrawLine(xdev->dpy, xdev->dest, xdev->gc, x0, y0, x1, y1);
  786.     if (xdev->bpixmap != (Pixmap) 0) {
  787.     int x = x0, y = y0, w = x1 - x0, h = y1 - y0;
  788.  
  789.     if (w < 0) x = x1, w = -w;
  790.     if (h < 0) y = y1, h = -h;
  791.     w++;
  792.     h++;
  793.     fit_fill(dev, x, y, w, h);
  794.     x_update_add(dev, x, y, w, h);
  795.     }
  796.     return 0;
  797. }
  798.  
  799. /* Set the device properties.  We reimplement this so we can resize */
  800. /* the window and avoid closing and reopening the device. */
  801. private const gs_prop_item x_props[] =
  802. {
  803.     prop_def("HWResolution", prt_float_array),
  804.     prop_def("HWSize", prt_int_array),
  805.         /* Slots for arrays */
  806.     prop_float, prop_float,
  807.     prop_int, prop_int,
  808. };
  809.  
  810. private int
  811. x_put_props(gx_device *dev, gs_prop_item *plist, int count)
  812. {
  813.     gx_device_X *xdev = (gx_device_X *)dev;
  814.     gs_prop_item *known[2];
  815.     int code = 0;
  816.     gx_device_X temp_dev;
  817.  
  818.     temp_dev = *xdev;
  819.     props_extract(plist, count, x_props, 2, known, 0);
  820.     if (known[1] != 0) {
  821.     if (known[1]->value.a.size != 2)
  822.         known[1]->status = pv_typecheck,
  823.         code = gs_error_typecheck;
  824.     else {
  825.         gs_prop_item *ap = known[1]->value.a.p.v;
  826.  
  827.         if (ap[0].value.i <= 0 || ap[0].value.i > 0x7fff ||
  828.         ap[1].value.i <= 0 || ap[1].value.i > 0x7fff)
  829.         known[1]->status = pv_rangecheck,
  830.         code = gs_error_rangecheck;
  831.         else {
  832.         temp_dev.width = ap[0].value.i;
  833.         temp_dev.height = ap[1].value.i;
  834.         }
  835.     }
  836.     }
  837.     if (known[0] != 0) {
  838.     if (known[0]->value.a.size != 2)
  839.         known[0]->status = pv_typecheck,
  840.         code = gs_error_typecheck;
  841.     else {
  842.         gs_prop_item *ap = known[0]->value.a.p.v;
  843.  
  844.         if (ap[0].value.f <= 0 || ap[1].value.f <= 0)
  845.         known[0]->status = pv_rangecheck,
  846.         code = gs_error_rangecheck;
  847.         else {
  848.         temp_dev.x_pixels_per_inch = ap[0].value.f;
  849.         temp_dev.y_pixels_per_inch = ap[1].value.f;
  850.         }
  851.     }
  852.     }
  853.     if (code < 0) return_error(code);
  854.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  855.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  856.     dev->width = temp_dev.width;
  857.     dev->height = temp_dev.height;
  858.     /* If the device is open, resize the window. */
  859.     /* Don't do this if Ghostview is active. */
  860.     if (dev->is_open && (known[0] != 0 || known[1] != 0) && !xdev->ghostview) {
  861.     XResizeWindow(xdev->dpy, xdev->win, dev->width, dev->height);
  862.     if (xdev->bpixmap != (Pixmap) 0) {
  863.         XFreePixmap(xdev->dpy, xdev->bpixmap);
  864.         xdev->bpixmap = (Pixmap) 0;
  865.     }
  866.     xdev->dest = 0;
  867.     gdev_x_clear_window(xdev);
  868.     }
  869.     return gx_default_put_props(dev, plist, count);
  870. }
  871.  
  872.  
  873. /* ------ Screen update procedures ------ */
  874.  
  875. /* Flush updates to the screen if needed. */
  876. private void
  877. update_do_flush(register gx_device *dev)
  878. {
  879.     gx_device_X *xdev = (gx_device_X *)dev;
  880.     int xo = xdev->update.xo, yo = xdev->update.yo;
  881.  
  882.     set_function(GXcopy);
  883.     XCopyArea(xdev->dpy, xdev->bpixmap, xdev->win, xdev->gc,
  884.           xo, yo, xdev->update.xe - xo, xdev->update.ye - yo,
  885.           xo, yo);
  886.     update_init(dev);
  887. }
  888.  
  889. /* Add a region to be updated. */
  890. /* This is only called if xdev->bpixmap != 0. */
  891. void
  892. x_update_add(register gx_device *dev, int xo, int yo, int w, int h)
  893. {
  894.     gx_device_X *xdev = (gx_device_X *)dev;
  895.     int xe = xo + w, ye = yo + h;
  896.     long new_area = (long)w * h;
  897.  
  898.     ++xdev->up_count;
  899.     if (xdev->up_area != 0) {    /* See whether adding this rectangle */
  900.     /* would result in too much being copied unnecessarily. */
  901.     long old_area = xdev->up_area;
  902.     long new_up_area;
  903.     rect u;
  904.  
  905.     u.xo = min(xo, xdev->update.xo);
  906.     u.yo = min(yo, xdev->update.yo);
  907.     u.xe = max(xe, xdev->update.xe);
  908.     u.ye = max(ye, xdev->update.ye);
  909.     new_up_area = (long)(u.xe - u.xo) * (u.ye - u.yo);
  910.     if (new_up_area > 100 &&
  911.         old_area + new_area < new_up_area * 2 / 3 ||
  912.         xdev->up_count >= 200)
  913.         update_do_flush(dev);
  914.     else {
  915.         xdev->update = u;
  916.         xdev->up_area = new_up_area;
  917.         return;
  918.     }
  919.     }
  920.     xdev->update.xo = xo;
  921.     xdev->update.yo = yo;
  922.     xdev->update.xe = xe;
  923.     xdev->update.ye = ye;
  924.     xdev->up_area = new_area;
  925. }
  926.  
  927. /* ------ Internal procedures ------ */
  928.  
  929. /* Substitute for XPutImage using XFillRectangle. */
  930. /* This is a total hack to get around an apparent bug */
  931. /* in some X servers.  It only works with the specific */
  932. /* parameters (bit/byte order, padding) used above. */
  933. private void
  934. alt_put_image(gx_device *dev, Display *dpy, Drawable win, GC gc,
  935.       XImage *pi, int sx, int sy, int dx, int dy, unsigned w, unsigned h)
  936. {
  937.     gx_device_X *xdev = (gx_device_X *)dev;
  938.     int raster = pi->bytes_per_line;
  939.     byte *data = (byte *) pi->data + sy * raster + (sx >> 3);
  940.     int init_mask = 0x80 >> (sx & 7);
  941.     int invert = 0;
  942.     int yi;
  943.  
  944. #define nrects 40
  945.     XRectangle rects[nrects];
  946.     XRectangle *rp = rects;
  947.  
  948.     XGCValues gcv;
  949.  
  950.     XGetGCValues(dpy, gc, (GCFunction | GCForeground | GCBackground), &gcv);
  951.  
  952.     if (gcv.function == GXcopy) {
  953.     XSetForeground(dpy, gc, gcv.background);
  954.     XFillRectangle(dpy, win, gc, dx, dy, w, h);
  955.     XSetForeground(dpy, gc, gcv.foreground);
  956.     } else if (gcv.function == GXand) {
  957.     if (gcv.background != ~(x_pixel) 0) {
  958.         XSetForeground(dpy, gc, gcv.background);
  959.         invert = 0xff;
  960.     }
  961.     } else if (gcv.function == GXor) {
  962.     if (gcv.background != 0) {
  963.         XSetForeground(dpy, gc, gcv.background);
  964.         invert = 0xff;
  965.     }
  966.     } else {
  967.     lprintf("alt_put_image: unimplemented function.\n");
  968.     exit(1);
  969.     }
  970.  
  971.     for (yi = 0; yi < h; yi++, data += raster) {
  972.     register int mask = init_mask;
  973.     register byte *dp = data;
  974.     register int xi = 0;
  975.  
  976.     while (xi < w) {
  977.         if ((*dp ^ invert) & mask) {
  978.         int xleft = xi;
  979.  
  980.         if (rp == &rects[nrects]) {
  981.             XFillRectangles(dpy, win, gc, rects, nrects);
  982.             rp = rects;
  983.         }
  984.         /* Scan over a run of 1-bits */
  985.         rp->x = dx + xi, rp->y = dy + yi;
  986.         do {
  987.             if (!(mask >>= 1))
  988.             mask = 0x80, dp++;
  989.             xi++;
  990.         } while (xi < w && (*dp & mask));
  991.         rp->width = xi - xleft, rp->height = 1;
  992.         rp++;
  993.         } else {
  994.         if (!(mask >>= 1))
  995.             mask = 0x80, dp++;
  996.         xi++;
  997.         }
  998.     }
  999.     }
  1000.     XFillRectangles(dpy, win, gc, rects, rp - rects);
  1001.     if (invert) XSetForeground(dpy, gc, gcv.foreground);
  1002. }
  1003.